#include <iostream>
#include "smarthome-server.h"
#include "cmd.h"
#include <netinet/in.h>
#include <map>

myDatabase *pMainDB;
myDatabase *pUsrDB;
myDatabase *pDevDB;
SmtPlug *pPlug;

int devid;

#include <time.h>
#include <sys/time.h>
#include <boost/lexical_cast.hpp>
#include <boost/unordered_map.hpp>
#include <boost/progress.hpp>

using namespace std;


int main()
{

#if 0

	uint8 szValue[13]="123532543";

	IdPswdMap m;
	Pswd_t pswdt;
	pswdt.pswd=(char*)szValue;
	pswdt.inSetingPswd=false;
	pswdt.cnt=0;
	m.insert(make_pair(10002,pswdt));


	 for(IdPswdMap::iterator it = m.find(10002); it != m.end(); it++)
	{
		 std::cout<<it->first<<","<<it->second.pswd<<endl;
	}
return 0;
#endif
/*
	IdMap m;
	m.insert(IdMap::value_type(12,123));

	m.insert(IdMap::value_type(12,156));
	m.insert(IdMap::value_type(16,156));
	m.insert(IdMap::value_type(15,156));
	m.insert(IdMap::value_type(12,123));
	//m.insert(123,125);
	//m.insert(123,126);
	cout<<m.size()<<endl;

    for (IdMap::const_iterator it = m.begin();
        it != m.end(); ++it)
        std::cout << " [" << it->first << ", " << it->second << "]";
    std::cout << std::endl;


    std::cout << "find('A') == "
        << std::boolalpha << (m.find('A') != m.end()) << std::endl;



	 for(IdMap::iterator it = m.find(16); it != m.end(); it++)
	{
		 std::cout<<it->first<<","<<it->second<<endl;
	}
	 return 0;
	 */
/*
    umMap::iterator it = m.find(12);


    std::cout << "find(12) == "
        << std::boolalpha << (it != m.end())
        << ": [" << it->first << ", " << it->second << "]" << std::endl;



	 for(umMap::iterator it = m.find(12); it != m.end(); it++)
	{
		 std::cout<<it->first<<","<<it->second<<endl;
	}
	 return 0;
	 */
/*
boost::progress_timer t;
	umap m;
	DataType1_t data;
	data.devAddr.sin_port=1;
	m[1010]=data;

	data.devAddr.sin_port=1;
	m[1011]=data;

	data.devAddr.sin_port=2;
	m[1012]=data;

	data.devAddr.sin_port=3;
	m[1012]=data;

	data.devAddr.sin_port=36;
	m[1012]=data;

	cout<<m.size()<<endl;

	 for(umap::iterator iter = m.begin(); iter != m.end(); iter++)
	{
		cout<<iter->first<<"\t"<<iter->second.devAddr.sin_port<<endl;
		//cout<<iter->second.devAddr.sin_port<<"\t"<<endl;
		boost::progress_timer t1;
	}

	 cout<<m[1012].devAddr.sin_port<<endl;;

	  return 0;
*/
	    /*

	DataType1_t data;
	map[1010]=data;

	   using boost::lexical_cast;
	    int a = lexical_cast<int>("123");
	    double b = lexical_cast<double>("123.12");
	    std::cout<<a<<std::endl;
	    std::cout<<b<<std::endl;
	    return 0;

*/

	//map<string,int> elem;
/*
	struct sockaddr_in a1,a2,a3,a4,a5;
	a1.sin_port=1;
	a2.sin_port=2;
	a3.sin_port=3;
	a4.sin_port=4;
	a5.sin_port=5;

	struct sockaddr_in u1,u2,u3,u4,u5;
	u1.sin_port=11;
	u2.sin_port=12;
	u3.sin_port=13;
	u4.sin_port=14;
	u5.sin_port=15;

	Hash hs;
	hs.CreateHt(6);
	hs.FillHtWithDevAddr(10001,a1);
	hs.FillHtWithDevAddr(10002,a2);
	hs.FillHtWithDevAddr(10003,a3);
	hs.FillHtWithDevAddr(10004,a4);
	hs.FillHtWithDevAddr(10005,a5);


	hs.DisplayHash(6);
	hs.FillHtWithUsrAddr(10003,u3);
	hs.FillHtWithUsrAddr(10002,u2);
	hs.FillHtWithUsrAddr(10002,u3);
	hs.FillHtWithUsrAddr(10002,u5);
	hs.FillHtWithUsrAddr(10002,u1);
	hs.FillHtWithUsrAddr(10002,u4);
	hs.FillHtWithUsrAddr(10002,u2);
	hs.DisplayHash(6);
	//hs.EraseDevId(10002);
	hs.EraseUsrAddr(10002,u5);
	hs.DisplayHash(6);

	struct sockaddr_in* puAddr;
	puAddr=hs.GetUsrAddrByDevId(10002);
	cout<<puAddr[0].sin_port<<endl;
	cout<<puAddr[1].sin_port<<endl;
	cout<<puAddr[2].sin_port<<endl;
	cout<<puAddr[3].sin_port<<endl;
	cout<<puAddr[4].sin_port<<endl;


return 0;
*/
	try{
		//start the database
//设备线程3链接的数据库

		myDatabase mydatabase;
		pMainDB=&mydatabase;

		int i=0;
		for(i=0;i<10;i++)
		{
			//可能mysql服务还没启动
			if(pMainDB->open(HOST,USER,PASSWORD,DBNAME))
				break;
			else
			{
				sleep(1);
				DBG("retry connect database\n");
			}
		}


#if 1
		SmtPlug Plug;
		pPlug=&Plug;
		pPlug->StartWork();
#endif

		pause();
		pMainDB->close();
		delete pMainDB;

		//close



	}
	catch (Exception &e)
	{
		e.ShowReason();

		perror("\n");
	}


	return 0;

}

